using System;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace Oracle.ManagedDataAccess.Types
{
	internal static class TimeSpanConv
	{
		internal const int FSecondsPerMilliSecond = 1000000;

		internal const int FSecondsPerSecond = 1000000000;

		internal const int FSecondsPerTick = 100;

		internal const double TicksPerFSecond = 0.01;

		internal static string ToString(byte[] byteRep, int yearPrec)
		{
			if (ProviderConfig.m_bTraceLevelPublic)
			{
				Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Entry, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString);
			}
			try
			{
				if (byteRep != null)
				{
					bool flag = true;
					int num = OracleIntervalYM.GetIYMData(byteRep, 0);
					int num2 = OracleIntervalYM.GetIYMData(byteRep, 1);
					if (num < 0)
					{
						flag = false;
						num = -num;
					}
					if (num2 < 0)
					{
						flag = false;
						num2 = -num2;
					}
					StringBuilder stringBuilder = new StringBuilder(15);
					string text = num.ToString();
					string text2 = num2.ToString();
					int length = text.Length;
					if (length < yearPrec)
					{
						text = HelperClass.GetZeros(yearPrec - length) + text;
					}
					else if (length > yearPrec)
					{
						text = text.Substring(0, yearPrec);
					}
					if (num2 < 10)
					{
						text2 = "0" + text2;
					}
					if (flag)
					{
						stringBuilder.Append("+");
					}
					else
					{
						stringBuilder.Append("-");
					}
					if (text.Length == 0)
					{
						stringBuilder.Append("0");
					}
					else
					{
						stringBuilder.Append(text);
					}
					stringBuilder.Append("-");
					stringBuilder.Append(text2);
					return stringBuilder.ToString();
				}
				return null;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Public, OracleTraceTag.Error, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Exit, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString);
				}
			}
		}

		internal static string ToString(byte[] byteRep, int dayPrec, int fSecondPrec)
		{
			if (ProviderConfig.m_bTraceLevelPublic)
			{
				Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Entry, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString);
			}
			try
			{
				if (byteRep != null)
				{
					bool flag = true;
					OracleIntervalDS.GetIDSData(byteRep, out var days, out var hours, out var minutes, out var seconds, out var fseconds);
					if (days < 0)
					{
						flag = false;
						days = -days;
						hours = -hours;
						minutes = -minutes;
						seconds = -seconds;
						fseconds = -fseconds;
					}
					else if (hours < 0)
					{
						flag = false;
						hours = -hours;
						minutes = -minutes;
						seconds = -seconds;
						fseconds = -fseconds;
					}
					else if (minutes < 0)
					{
						flag = false;
						minutes = -minutes;
						seconds = -seconds;
						fseconds = -fseconds;
					}
					else if (seconds < 0)
					{
						flag = false;
						seconds = -seconds;
						fseconds = -fseconds;
					}
					else if (fseconds < 0)
					{
						flag = false;
						fseconds = -fseconds;
					}
					string text = days.ToString();
					string text2 = fseconds.ToString();
					if (text.Length < dayPrec)
					{
						text = HelperClass.GetZeros(dayPrec - text.Length) + text;
					}
					int length = text2.Length;
					if (length < fSecondPrec)
					{
						text2 = HelperClass.GetZeros(fSecondPrec - length) + text2;
					}
					else if (length > fSecondPrec)
					{
						text2 = text2.Substring(0, fSecondPrec);
					}
					int length2 = text.Length;
					if (length2 < dayPrec)
					{
						text = HelperClass.GetZeros(dayPrec - length2) + text;
					}
					else if (length2 > dayPrec)
					{
						text = text2.Substring(0, dayPrec);
					}
					StringBuilder stringBuilder = new StringBuilder(30);
					if (flag)
					{
						stringBuilder.Append("+");
					}
					else
					{
						stringBuilder.Append("-");
					}
					if (text.Length == 0)
					{
						stringBuilder.Append("0");
					}
					else
					{
						stringBuilder.Append(text);
					}
					stringBuilder.Append(" ");
					if (hours <= 9)
					{
						stringBuilder.Append("0");
					}
					stringBuilder.Append(hours);
					stringBuilder.Append(":");
					if (minutes <= 9)
					{
						stringBuilder.Append("0");
					}
					stringBuilder.Append(minutes);
					stringBuilder.Append(":");
					if (seconds <= 9)
					{
						stringBuilder.Append("0");
					}
					stringBuilder.Append(seconds);
					if (text2 != "")
					{
						stringBuilder.Append(".");
						stringBuilder.Append(text2);
					}
					return stringBuilder.ToString();
				}
				return null;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Public, OracleTraceTag.Error, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Exit, OracleTraceClassName.TimeSpanConv, OracleTraceFuncName.ToString);
				}
			}
		}
	}
}
